# coding: utf-8
import json

from requests_toolbelt import MultipartEncoder

from common import BaseAPI, Timestamp, cfg


class LoginCodeAPI(BaseAPI):
    base_url = 'https://www.qingshuxuetang.com'

    @classmethod
    def getValidationCode(cls, phone: str, validation_type: int):
        """
        获取手机验证码
        :param phone: 手机号
        :param validation_type: 验证码类型 1:验证码登录 2: 扫码登录 3: 账号密码登录
        :return:
        """
        url = '/Svc/GetValidationCode'
        params = {'_t': Timestamp.now_ms()}
        payload = {
            "recv": phone,
            "validationType": validation_type
        }
        response = cls.post(url, params=params, json=payload)
        return response.json()

    @classmethod
    def getQRCode(cls):
        """
        获取二维码
        :return:
        """
        url = '/GetQRCode'
        params = {'_t': Timestamp.now_ms(), '_': Timestamp.now_ms()}
        response = cls.get(url, params=params)
        return response.json()

    @classmethod
    def getQRCodeStatus(cls, code: str):
        url = '/GetQRCodeStatus'
        params = {'_t': Timestamp.now_ms(), '_': Timestamp.now_ms(), 'code': code}
        response = cls.get(url, params=params)
        return response.json()


class LoginAPI(BaseAPI):
    base_url = 'https://www.qingshuxuetang.com'
    deviceInfoQS = {
        "netType": 1,
        "appType": 3,
        "clientType": 3,
        "deviceName": "PCWeb",
        "osVersion": "Win32",
        "appVersion": "5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/140.0.0.0+Safari/537.36+Edg/140.0.0.0",
        "imei": "",
        "mac": "",
        "idfa": ""
    }

    @classmethod
    def loginByAuthCode(cls, phone: str, userInput: str, smsToken: str):
        """
        手机验证码登录
        :return:
        """
        url = '/LoginByAuthCode'
        params = {'_t': Timestamp.now_ms()}
        payload = {
            "phone": phone,
            "userInput": userInput,
            "smsToken": smsToken,
            "ref": 'https://degree.qingshuxuetang.com/sias/Student/Home',
            "deviceInfoQS": json.dumps(cls.deviceInfoQS),
        }
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        response = cls.post(url, params=params, data=payload)
        return response.json()

    @classmethod
    def loginByQRCode(cls, code: str):
        """
        二维码扫码登录
        :return:
        """
        url = '/LoginByQRCode'
        params = {'_t': Timestamp.now_ms()}
        payload = {'code': code}
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        response = cls.post(url, params=params, data=payload, headers=headers)
        return response.headers

    @classmethod
    def login(cls, username: str, password: str, userInput: str, sessionId: str):
        """
        账号密码登录
        :param username:
        :param password:
        :param userInput:
        :param sessionId:
        :return:
        """
        url = '/Login'
        params = {'_t': Timestamp.now_ms()}
        payload = {
            "username": username,
            "password": password,
            "userInput": userInput,
            "sessionId": sessionId,
            "type": 3,
            "ref": "",
            "deviceInfoQS": json.dumps(cls.deviceInfoQS),
        }
        headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
        response = cls.post(url, params=params, data=payload, headers=headers)
        return response.headers

    @staticmethod
    def setCookies(headers: dict):
        cookie = headers.get('Set-Cookie')
        trace_id = headers.get('Trace-Id')
        cfg.set(cfg.cookie, cookie)
        cfg.set(cfg.traceId, trace_id)


if __name__ == '__main__':
    print(LoginCodeAPI.getQRCode())
